package util.preprocessing;

import java.sql.Timestamp;
import java.util.Hashtable;
import java.util.LinkedList;

import util.io.FileInput;

public class RefindingAnalysis {

	
	
	static final int DOMAIN_POS =5;
	static final int QUERY_POS	=2;
	static final int TIME_POS =3; 
	static final int RANK_POS =4;
	
	
	public static void printRefindingStats(String path, int groupBy) {

		FileInput in = new FileInput(path);

		String line = in.readString();

		String previous = "";
		LinkedList<String[]> list = new LinkedList<String[]>();
		while (line != null) {
			line = line.trim();
			String t[] = line.split("\t");

			String current = "";

			if (t.length > groupBy) {
				current = t[groupBy];

			}

			if (current.equals(previous)) {

				list.add(t);
			} else {

				// analyze current group
				analyzeReFindGroup(list);

				// init new group
				list = new LinkedList<String[]>();

				list.add(t);
			}

			previous = current;

			line = in.readString();
		}

		// analyze last group

		analyzeReFindGroup(list);

	}

	private static void analyzeReFindGroup(LinkedList<String[]> list) {
		// TODO Auto-generated method stub

		for (int i = 0; i < list.size(); i++) {
			boolean refindFound = false;
			int j = i + 2;

			Timestamp a =null;
			if(list.get(i).length> TIME_POS)
				a = java.sql.Timestamp.valueOf(list.get(i)[TIME_POS]
					.trim());

			while (!refindFound && j < list.size()
					&& list.get(i).length > DOMAIN_POS) {
				int rank_differece = 0;
				if (list.get(j).length > DOMAIN_POS
						&& list.get(i)[DOMAIN_POS].trim().equals(
								list.get(j)[DOMAIN_POS].trim())) {
					refindFound = true;
					Timestamp b = java.sql.Timestamp
							.valueOf(list.get(j)[TIME_POS].trim());
					double t = a.getTime() / 1000 - b.getTime() / 1000;
					Long time_difference = new Long((long) t);

					// rank difference

					if (list.get(j).length > RANK_POS && !list.get(i)[QUERY_POS].equals("-") && !list.get(j)[QUERY_POS].equals("-")
							&& list.get(i).length > RANK_POS && checkIfNumber(list.get(i)[RANK_POS]) && checkIfNumber(list.get(j)[RANK_POS])  )  {

						Integer rank_a = Integer.valueOf(list.get(i)[RANK_POS]);
						Integer rank_b = Integer.valueOf(list.get(j)[RANK_POS]);

						rank_differece = rank_a - rank_b;

					}

					if (time_difference != 0) {

						System.out.println(list.get(i)[QUERY_POS] + "\t"
								+ list.get(j)[QUERY_POS] + "\t"
								+ list.get(i)[DOMAIN_POS] + "\t"
								+ time_difference + "\t" + rank_differece
								+ "\t" + list.get(i)[RANK_POS] + "\t"
								+ list.get(j)[RANK_POS]

						);

					}

				}
				j++;
			}

		}

	}
	
	
	  public static  boolean checkIfNumber(String in) {
	        
	        try {

	            Integer.parseInt(in);
	        
	        } catch (NumberFormatException ex) {
	            return false;
	        }
	        
	        return true;
	    }

	public static void cleanNoDMOZ(String path, String dmoz_path, int domain_pos) {

		FileInput in = new FileInput(dmoz_path);
		Hashtable<String, String> dmoz = new Hashtable<String, String>();
		String line = in.readString();

		while (line != null) {
			dmoz.put(line.trim(), "");

			line = in.readString();

		}

		FileInput in1 = new FileInput(path);

		line = in1.readString();

		while (line != null) {

			if (dmoz.containsKey(line.split("\t")[domain_pos].trim())){
				
				
				System.out.println(line);
			}

				line = in1.readString();
		}

	}

	public static void main(String argsp[]){
		String path = "/home/sergio/projects/data/AOL_queryLOG/sessions_contains_kidsCleaned.txt";
		
		path = "/home/sergio/projects/data/AOL_queryLOG/sessions_cleaned.txt";
		
		String dmoz = "/home/sergio/projects/data/AOL_queryLOG/dmoz_domains_kids.txt";
		
		String refind= "/home/sergio/projects/data/AOL_queryLOG/refinding_kids_session.txt";
		
		printRefindingStats(path,1);
		
		
		
		//cleanNoDMOZ(refind,dmoz,2);
	}
	
	
}
